package selenium;

/**
 * @author tmjee
 * @version $Date $Id
 */
public class LoginAsAdminSeleniumTestCase extends AbstractSeleniumTestCase {

    public void setUp() throws Exception {
        super.setUp();
        setUp(getDefaultBaseUrl(), getDefaultBrowserName());
        selenium.open("/wiki/general/Dashboard.action?view=");
        clickAndWait("link=Login");
        selenium.type("username", "admin");
        selenium.type("password", "admin");
        clickAndWait("login");
    }

    public void tearDown() throws Exception {
        //selenium.click("userHeaderMenuButton-button");
        //selenium.click("link=Logout");   // NOTE: for some reason, selenium server can't detect this element, in SeleniumIDE it seems ok.
        selenium.open("/wiki/user/Logout.action?logout=");
        selenium.close();
        super.tearDown();                                                     
    }


    public void test_loginAsAdmin() throws Exception {
        assertTrue(selenium.isElementPresent("//meta[@name='_uniquePageId_'][@content='_dashboard_']"));
    }

    public void test_adminDashboard() throws Exception {
        selenium.open("/wiki/general/Dashboard.action?view=");
        assertTrue(selenium.isElementPresent("//meta[@name='_uniquePageId_'][@content='_dashboard_']"));
    }

    public void test_listUsers() throws Exception {
        selenium.open("/wiki/user/ListUsers.action?view=");
        assertTrue(selenium.isElementPresent("//meta[@name='_uniquePageId_'][@content='_listUsers_']"));
        selenium.click("id=_hideDescription");
        assertFalse(selenium.isTextPresent("Following are the listing of all users that are currently available in FooBarWiki"));
        selenium.click("id=_showDescription");
        assertTrue(selenium.isTextPresent("Following are the listing of all users that are currently available in FooBarWiki"));
    }

    public void test_viewFavouritePages() throws Exception {
        selenium.open("/wiki/general/Dashboard.action?view=");
        clickAndWait("xpath=//table[@id='favouritePagesTableElement']/tbody/tr[1]/td[3]/a[1]");
        assertTrue(selenium.isElementPresent("//meta[@name='_uniquePageId_'][@content='_page_']"));
    }

    public void test_viewFavouritePagesRss() throws Exception {
        selenium.open("/wiki/general/Dashboard.action?view=");
        clickAndWait("xpath=//table[@id='favouritePagesTableElement']/tbody/tr[1]/td[3]/a[3]");
        assertFalse(selenium.isElementPresent("xpath=//meta[@name='_uniquePageId_'][@content='_uncaughtException_']"));
    }

    public void test_viewFavouritePagesAtom() throws Exception {
        selenium.open("/wiki/general/Dashboard.action?view=");
        clickAndWait("xpath=//table[@id='favouritePagesTableElement']/tbody/tr[1]/td[3]/a[4]");
        assertFalse(selenium.isElementPresent("xpath=//meta[@name='_uniquePageId_'][@content='_uncaughtException_']"));
    }


    public void test_manageSpace() throws Exception {
        clickAndWait("xpath=id('spacesTableElement')/tbody/tr[1]/td[3]/a[2]"); // click on space 'manage' link
        assertTrue(selenium.isElementPresent("xpath=//meta[@name='_uniquePageId_'][@content='_editSpace_']"));
        clickAndWait("link=View");
        assertTrue(selenium.isElementPresent("xpath=//meta[@name='_uniquePageId_'][@content='_viewSpace_']"));
        clickAndWait("link=Edit");      
        assertTrue(selenium.isElementPresent("xpath=//meta[@name='_uniquePageId_'][@content='_editSpace_']"));
    }

    public void test_manageRecentUpdatePage() throws Exception {
        clickAndWait("xpath=id('recentUpdatesTableElement')/tbody/tr[1]/td[3]/a[2]"); // click on recent update 'manage' link
        assertTrue(selenium.isElementPresent("xpath=//meta[@name='_uniquePageId_'][@content='_editPage_]"));
        clickAndWait("link=View");
        assertTrue(selenium.isElementPresent("xpath=//meta[@name='_uniqueId_'][@content='_viewPage_]"));
        clickAndWait("link=Edit");
        assertTrue(selenium.isElementPresent("xpath=//meta[@name='_uniqueId_'][@content='_editPage_]"));
    }

    public void test_manageFavouritePage() throws Exception {
        clickAndWait("xpath=id('favouritePagesTableElement')/tbody/tr[1]/td[3]/a[2]");
        assertTrue(selenium.isElementPresent("xpath=//meta[@name='_uniquePageId_'][@name='_viewPage_']"));
        clickAndWait("link=View");
        assertTrue(selenium.isElementPresent("xpath=//meta[@name='_uniquePageId_'][@name='_editPage_']"));
        clickAndWait("link=Edit");
    }

    public void test_manageSpaceMembership() throws Exception {
        clickAndWait("xpath=id('spacesTableElement')/tbody/tr[1]/td[3]/a[4]");
        assertTrue(selenium.isElementPresent("xpath=//meta[@name='_uniquePageId_'][@content='_editSpaceMembership_']"));
        selenium.check("id=toBeMembers_2");
        clickAndWait("name=addSpaceMembership");
        assertTrue(selenium.isElementPresent("xpath=//meta[@name='_uniquePageId_'][@content='_editSpaceMembership_']"));
        assertTrue(selenium.isTextPresent("Memberships added successfully"));
        selenium.check("id=toBeNonMembers_2");
        clickAndWait("name=removeSpaceMembership");
        assertTrue(selenium.isElementPresent("xpath=//meta[@name='_uniquePageId_'][@content='_editSpaceMembership_']"));
        assertTrue(selenium.isTextPresent("Memberships removed successfully"));
    }
}
